import { _decorator, Component, UIOpacity } from "cc";

const { ccclass, property } = _decorator;

@ccclass
export class ItemAttribute extends Component {

    @property({ displayName: "角度变化速度" })
    angleSpeed: number = 0; // 旋转速度

    @property({ displayName: "透明度变化速度" })
    opacitySpeed: number = 0; // 旋转速度

    @property({ displayName: "透明度最小值" })
    opacityMin: number = 0; // 旋转速度

    @property({ displayName: "透明度最大值" })
    opacityMax: number = 0; // 旋转速度

    /** 透明度改变方向 */
    opacityDir: number = 0;

    /** 是否启动 */
    private _lunch: boolean = true;
    get lunch(){
        return this._lunch;
    }
    set lunch(l: boolean){
        this._lunch = l;
    }

    protected update(dt: number): void {
        if (!this.lunch) {
            return;
        }
        
        // 每帧旋转
        this.updateAngle(dt);
        // 每帧透明度变化
        this.updateOpacity(dt);
    }

    /**
     * 更新角度
     * @param dt 
     */
    updateAngle(dt: number) {
        if (this.angleSpeed == 0) {
            return;
        }
        this.node.angle -= this.angleSpeed;
    }

    /**
     * 更新透明度；
     * @param dt 
     * @returns 
     */
    updateOpacity(dt: number) {
        if (this.opacitySpeed == 0) {
            return;
        }
        let _uiOpacity = this.node.getComponent(UIOpacity);
        let opacity = _uiOpacity.opacity;
        if (this.opacityDir == 0) {
            opacity += this.opacitySpeed;
            if (opacity >= this.opacityMax) {
                opacity = this.opacityMax;
                this.opacityDir = 1;
            }
        }
        else {
            opacity -= this.opacitySpeed;
            if (opacity <= this.opacityMin) {
                opacity = this.opacityMin;
                this.opacityDir = 0;
            }
        }
        this.node.getComponent(UIOpacity).opacity = opacity;
    }
}
